package com.avic.rong.utils;

import android.content.Context;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.net.wifi.WifiInfo;
import android.net.wifi.WifiManager;
import android.telephony.TelephonyManager;
import android.text.TextUtils;

import com.orhanobut.logger.Logger;
import com.umeng.commonsdk.statistics.common.DeviceConfig;

import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

/**
 * Creater: Created by wangyz on 2017/3/13.
 * Email: 936804097@qq.com
 */

public class DeviceUtils {


    /**
     * 国际移动设备身份码 获取手机IMEI,如果Imei为空则获取mac地址
     *
     * @param context
     * @return
     */
    public static final String getIMEString(Context context) {


        String newDeviceId = SharedPreferencesUtil.getInstance().getString(ConstantsUtils.UUID, ConstantsUtils.EMPTY_STR);
        if (!TextUtils.isEmpty(newDeviceId)) {
            return newDeviceId;
        } else {
            try {
                TelephonyManager telephonyManager = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
                String imei = telephonyManager.getDeviceId();
                if (TextUtils.isEmpty(imei)) {
                    imei = UUID.randomUUID().toString();
                }
                SharedPreferencesUtil.getInstance().put(ConstantsUtils.UUID, imei);
                return imei;
            } catch (Exception e) {
                String createDeviceId = UUID.randomUUID().toString();
                SharedPreferencesUtil.getInstance().put(ConstantsUtils.UUID, createDeviceId);
                return createDeviceId;
            }
        }

    }


    /**
     * 获取手机MAC地址
     *
     * @return
     */
    public static String getMacAddress(Context context) {
        try {
            WifiManager wifiManager = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
            WifiInfo wifiInfo = wifiManager.getConnectionInfo();
            String mac = wifiInfo.getMacAddress();
            if (null == mac) {
                mac = "00000000";
            }
            return mac;
        } catch (Exception e) {
            e.printStackTrace();
            return "00000000";
        }
    }

    public static String getAppMetaData(Context ctx, String key) {
        if (ctx == null || TextUtils.isEmpty(key)) {
            return null;
        }
        String resultData = null;
        try {
            PackageManager packageManager = ctx.getPackageManager();
            if (packageManager != null) {
                ApplicationInfo applicationInfo = packageManager.getApplicationInfo(ctx.getPackageName(), PackageManager.GET_META_DATA);
                if (applicationInfo != null) {
                    if (applicationInfo.metaData != null) {
                        resultData = applicationInfo.metaData.getString(key);
                    }
                }
            }
        } catch (PackageManager.NameNotFoundException e) {
            e.printStackTrace();
        }

        return resultData;
    }

    /**
     * 获取版本号
     *
     * @return 当前应用的版本号
     */
    public static String getVersionName(Context context) {
        try {
            PackageManager manager = context.getPackageManager();
            PackageInfo info = manager.getPackageInfo(context.getPackageName(), 0);
            String version = info.versionName;
            return version;
        } catch (Exception e) {
            e.printStackTrace();
            return "0.0";
        }
    }

    /**
     * 获取版本号
     *
     * @return 当前应用的版本号
     */
    public static int getVersionCode(Context context) {
        try {
            PackageManager manager = context.getPackageManager();
            PackageInfo info = manager.getPackageInfo(context.getPackageName(), 0);
            int version = info.versionCode;
            return version;
        } catch (Exception e) {
            e.printStackTrace();
            return 0;
        }
    }

    /**
     * 获取当前手机系统版本号
     *
     * @return 系统版本号
     */
    public static String getSystemVersion() {
        return "android-" + android.os.Build.VERSION.RELEASE;
    }

    /**
     * 获取手机型号
     *
     * @return 手机型号
     */
    public static String getSystemModel() {
        return android.os.Build.MODEL;
    }

    /**
     * 获取网络提供商
     *
     * @param context
     * @return
     */
    public static String getNetOperator(Context context) {
        TelephonyManager telephonyManager = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);

        if (!TextUtils.isEmpty(telephonyManager.getNetworkOperator())) {
            String netOperator = telephonyManager.getNetworkOperator();
            if (netOperator.startsWith("46000")) {
                netOperator = "中国移动(46000)";
            } else if (netOperator.startsWith("46002")) {
                netOperator = "中国移动(46002)";
            } else if (netOperator.startsWith("46007")) {
                netOperator = "中国移动(46007)";
            } else if (netOperator.startsWith("46001")) {
                netOperator = "中国联通(46001)";
            } else if (netOperator.startsWith("46006")) {
                netOperator = "中国联通(46006)";
            } else if (netOperator.startsWith("46003")) {
                netOperator = "中国电信(46003)";
            } else if (netOperator.startsWith("46005")) {
                netOperator = "中国电信(46005)";
            } else if (netOperator.startsWith("46011")) {
                netOperator = "中国电信(46011)";
            } else {
                netOperator = telephonyManager.getNetworkOperatorName() + "(" + telephonyManager.getNetworkOperator() + ")";
            }
            return netOperator;
        } else {
            return ConstantsUtils.EMPTY_STR;
//            return getProviders(context, telephonyManager);
        }
    }

    private static String getProviders(Context context, TelephonyManager tm) {
        ConnectivityManager connectivityManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
        String providerStr = ConstantsUtils.EMPTY_STR;
        String net = getNetWork(connectivityManager);
        List<String> infos = getNetWorkList(connectivityManager);

        if (net == null || net.equalsIgnoreCase("WIFI")) {
            if (infos.size() > 1) {
                infos.remove("WIFI");
                net = infos.get(0);
                if (net.equals("3gwap") || net.equals("uniwap") || net.equals("3gnet") || net.equals("uninet")) {
                    providerStr = "中国联通";
                } else if (net.equals("cmnet") || net.equals("cmwap")) {
                    providerStr = "中国移动";
                } else if (net.equals("ctnet") || net.equals("ctwap")) {
                    providerStr = "中国电信";
                }
            } else {
                providerStr = getProvidersName(tm);
            }
        } else {
            if (net.equals("3gwap") || net.equals("uniwap") || net.equals("3gnet") || net.equals("uninet")) {
                providerStr = "中国联通";
            } else if (net.equals("cmnet") || net.equals("cmwap")) {
                providerStr = "中国移动";
            } else if (net.equals("ctnet") || net.equals("ctwap")) {
                providerStr = "中国电信";
            }
        }

        return providerStr;
    }

    public static List<String> getNetWorkList(ConnectivityManager cm) {
        NetworkInfo[] infos = cm.getAllNetworkInfo();
        List<String> list = new ArrayList<String>();
        if (infos != null) {
            for (int i = 0; i < infos.length; i++) {
                NetworkInfo info = infos[i];
                String name = null;
                if (info.getTypeName().equals("WIFI")) {
                    name = info.getTypeName();
                } else {
                    name = info.getExtraInfo();
                }
                if (name != null && list.contains(name) == false) {
                    list.add(name);
                }
            }
        }
        return list;
    }

    public static String getNetWork(ConnectivityManager connectivityManager) {
        String NOWNET = null;
        NetworkInfo info = connectivityManager.getActiveNetworkInfo();
        if (info != null && info.isAvailable()) {
            if (info.getTypeName().equals("WIFI")) {
                NOWNET = info.getTypeName();
            } else {
                NOWNET = info.getExtraInfo();// cmwap/cmnet/wifi/uniwap/uninet
            }
        }
        return NOWNET;
    }


    public static String getProvidersName(TelephonyManager telephonyManager) {
        String providersName = ConstantsUtils.EMPTY_STR;
        try {
            String subscriberId = telephonyManager.getSubscriberId();
            if (!TextUtils.isEmpty(subscriberId)) {
                if (subscriberId.startsWith("46000")) {
                    providersName = "中国移动(46000)";
                } else if (subscriberId.startsWith("46002")) {
                    providersName = "中国移动(46002)";
                } else if (subscriberId.startsWith("46007")) {
                    providersName = "中国移动(46007)";
                } else if (subscriberId.startsWith("46001")) {
                    providersName = "中国联通(46001)";
                } else if (subscriberId.startsWith("46006")) {
                    providersName = "中国联通(46006)";
                } else if (subscriberId.startsWith("46003")) {
                    providersName = "中国电信(46003)";
                } else if (subscriberId.startsWith("46005")) {
                    providersName = "中国电信(46005)";
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return providersName;
    }


    public static String[] getTestDeviceInfo(Context context){
        String[] deviceInfo = new String[2];
        try {
            if(context != null){
                deviceInfo[0] = DeviceConfig.getDeviceIdForGeneral(context);
                deviceInfo[1] = DeviceConfig.getMac(context);
                Logger.i("-----deviceid---------------" + deviceInfo + "--mac-" + deviceInfo[1]);
            }
        } catch (Exception e){
        }
        return deviceInfo;
    }

}
